package com.leonzhangxf.email.api;

import com.leonzhangxf.email.EmailApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author leonzhangxf
 */
@Slf4j
@Service
@org.apache.dubbo.config.annotation.Service
public class EmailApiImpl implements EmailApi {

    private JavaMailSender javaMailSender;

    @Override
    public Boolean sendEmail(String email, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("leon_zhangxf@qq.com");
        message.setTo(email);
        message.setSubject("注册验证码");
        message.setText(String.format("验证码为：%s", code));
        try {
            javaMailSender.send(message);
        } catch (Exception ex) {
            log.error("send email err. email: {}, ", email, ex);
            return false;
        }
        return true;
    }

    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }
}
